//
//  ItemSelectionViewController.m
//  CS Inventory Manager
//
//  Created by Ian Smith on 3/24/13.
//  Copyright (c) 2013 Transylvania University. All rights reserved.
//

#import "ItemSelectionViewController.h"
#import "PatronSelectionViewController.h"
#import "AppDelegate.h"
#import "Category.h"
#import "Type.h"
#import "Item.h"

@interface ItemSelectionViewController ()

@end

@implementation ItemSelectionViewController
@synthesize navBar, itemsTableView, checkoutItems, currentSelection;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//IBAction for the "Next" button
- (void)nextButton:(id)sender
{
    //Make sure an Item is selected before going to next screen
    if (currentSelection) {
        [self performSegueWithIdentifier:@"PatronSelection" sender:currentSelection];
    }
    else {
        //Don't go to next screen, because they haven't selected anything...
    }
}

//Pass along the selected Item to the next screen in the Checkout Wizard here
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    PatronSelectionViewController *nextViewController = segue.destinationViewController;
    nextViewController.selectedItem = sender;
}

//IBAction for the "Cancel" button
- (void)cancelButton:(id)sender
{
    //Cancel and go back to previous screen here
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navBar.topItem.title = @"Check Out Wizard";
    self.view.backgroundColor = [UIColor grayColor];
}
- (void)viewWillAppear:(BOOL)animated
{
    //Flattening the item hierarchy to get a list of items
    checkoutItems = [[NSMutableArray alloc] init];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"Checkout View Flattening pre-loaded Item hierarchy");
    for (int i = 0; i < appDelegate.categories.count; i++) {                                    //Iterate through Categories
        Category *category = appDelegate.categories[i];
        for (int j = 0; j < category.types.count; j++) {                                        //Iterate through Types
            Type *type = category.types[j];
            for (int k = 0; k < type.items.count; k++) {                                        //Iterate through Items
                Item *item = type.items[k];
                if (![item isCheckedOut]) {                                                     //Make sure Item is available to be checked out
                    [checkoutItems addObject:item];
                    NSLog(@"Added %@ to check-out view", item.model);
                    NSLog(@"Now have %lu items!", (unsigned long)checkoutItems.count);          //Log addition for reference and bug checking
                }
            }
        }
    }
    NSLog(@"Finished flattening hierarchy!");
    [self.itemsTableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return checkoutItems.count;
}

//Setup for the list of Items
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    Item *object = checkoutItems[indexPath.row];
    cell.textLabel.text = object.model;
    
    return cell;
}

#pragma mark - Table view delegate

//If item is tapped, simply add it as the selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    currentSelection = checkoutItems[indexPath.row];
}

@end
